<?php
declare (strict_types=1);

namespace app\common\model;

class UserAuth extends Base
{
    protected $type = [
        'examine_time' => 'timestamp'
    ];

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function getStatusNameAttr($value, $data): string
    {
        return getExamineStatus($data['status']);
    }

    public static function _save($param, $options): array
    {
        parent::startTrans();
        try {
            if (empty($param['status'])) {
                throw new \Exception('请选择审核类型');
            }
            $info = parent::where(['id' => $param['id'], 'status' => 1])->find();
            if (empty($info)) {
                throw new \Exception('该申请不存在或已审核');
            }
            if ($param['status'] == 2) {
                $info->reject_msg = '';
                User::update([
                    'real_name' => $info->real_name,
                    'id_card' => $info->id_card,
                    'img_front' => $info->img_front,
                    'img_back' => $info->img_back
                ], ['id' => $info->uid]);
                $content = '您的实名认证申请通过审核。';
                UserNotify::add(1, $info->uid, '实名认证', $content);
                $msg = '通过实名认证申请，标识ID【' . $param['id'] . '】，会员【' . $info->user->id . '】【' . $info->user->code . '】';
            } else {
                $info->reject_msg = $param['reject_msg'];
                $content = '您的实名认证申请已被驳回，请查看原因。';
                UserNotify::add(1, $info->uid, '实名认证', $content);
                $msg = '驳回实名认证申请，标识ID【' . $param['id'] . '】，会员【' . $info->user->id . '】【' . $info->user->code . '】，驳回原因【' . $param['reject_msg'] . '】';
            }
            $info->status = $param['status'];
            $info->admin_id = $options['admin']->id;
            $info->admin_name = $options['admin']->admin_name;
            $info->examine_time = time();
            $info->save();
            Logs::adds($msg, $options['admin']->id, $options['admin']->admin_name);
            parent::commit();

            return ['code' => 1, 'msg' => '审核成功'];
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function batchPass($param, $admin, $options): array
    {
        parent::startTrans();
        try {
            $list = parent::where(['status' => 1])->whereIn('id', $param['id'])->select();
            if ($list->isEmpty()) {
                throw new \Exception('未查询到待审核的数据，请检查！');
            }
            $new_ids = [];
            foreach ($list as $item) {
                User::update([
                    'real_name' => $item->real_name,
                    'id_card' => $item->id_card,
                    'img_front' => $item->img_front,
                    'img_back' => $item->img_back
                ], ['id' => $item->uid]);
                $new_ids[] = $item->id;
                $content = '您的实名认证申请通过审核。';
                UserNotify::add(1, $item->uid, '实名认证', $content);
            }
            if ($new_ids) {
                parent::update([
                    'status' => 2,
                    'examine_time' => time(),
                    'admin_id' => $admin->id,
                    'admin_name' => $admin->admin_name
                ], ['id' => $new_ids]);
                $msg = '批量通过实名认证申请，标识ID【' . implode(',', $new_ids) . '】';
                Logs::adds($msg, $admin->id, $admin->admin_name);
            }
            parent::commit();

            return ['code' => 1, 'msg' => '批量通过成功'];
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function batchReject($param, $admin, $options): array
    {
        parent::startTrans();
        try {
            $list = parent::where(['status' => 1])->whereIn('id', $param['id'])->select();
            if ($list->isEmpty()) {
                throw new \Exception('未查询到待审核的数据，请检查！');
            }
            $new_ids = [];
            foreach ($list as $item) {
                $new_ids[] = $item->id;
                $content = '您的实名认证申请已被驳回，请查看原因。';
                UserNotify::add(1, $item->uid, '实名认证', $content);
            }
            if ($new_ids) {
                parent::update([
                    'status' => 3,
                    'reject_msg' => $param['batchReject'],
                    'examine_time' => time(),
                    'admin_id' => $admin->id,
                    'admin_name' => $admin->admin_name
                ], ['id' => $new_ids]);
                $msg = '批量驳回实名认证申请，标识ID【' . implode(',', $new_ids) . '】，驳回原因【' . $param['batchReject'] . '】';
                Logs::adds($msg, $admin->id, $admin->admin_name);
            }
            parent::commit();

            return ['code' => 1, 'msg' => '批量驳回成功'];
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function add($param, $user): array
    {
        try {
            if (!empty($user->id_card)) {
                throw new \Exception('已认证，无需重新认证');
            }
            $is1 = parent::where(['uid' => $user->id, 'status' => 1])->count();
            if ($is1) {
                throw new \Exception('您已提交过申请，请耐心等待审核');
            }
            $is2 = parent::where(['id_card' => $param['id_card'], 'status' => 1])->count();
            if ($is2) {
                throw new \Exception('该身份证号已被申请，请更换');
            }
            $is3 = User::where(['id_card' => $param['id_card']])->count();
            if ($is3) {
                throw new \Exception('该身份证号已被认证，请更换');
            }
            $param['uid'] = $user->id;
            $param['status'] = 1;
            parent::create($param);

            return ['code' => 1, 'msg' => '认证申请已提交，请耐心等待审核'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}